<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css"
	href="../EXT4/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css"
	href="../css/style.css" />
<script type="text/javascript" src="../EXT4/ext-all.js"></script>
<script type="text/javascript" src="../script/ext-lang-zh_CN.js"></script>
<script type="text/javascript">
Ext.onReady(function() {
	
	Ext.QuickTips.init();
	
	Ext.define('User', { 
        extend: 'Ext.data.Model', 
        // fields定义字段和数据类型 
        fields: ['id', 'username', 'password','displayName','sex','status'] 
    });
	
	Ext.define('Subject', { 
        extend: 'Ext.data.Model', 
        // fields定义字段和数据类型 
        fields: ['id', 'displayName'] 
    });
	
	var subjectstore=Ext.create('Ext.data.Store', {
		model: 'Subject',
		autoload: true,
		proxy: {
			type: 'ajax',
			url: '../service/subject_allsub',
			reader: {
				type: 'json',
				root: 'root'
			}
		}
	});
	
	var agestore=Ext.create('Ext.data.Store', {
     fields: ['key','value'],
     data : [
         {key:1, value:'L1'},
         {key:2, value:'L2'},
         {key:3, value:'L3'},
		]
	});
	
	Ext.define('worktime', { 
        extend: 'Ext.data.Model', 
        // fields定义字段和数据类型 
        fields: [
                 {type: 'string', name: 'id'},
                 {type: 'string', name: 'worktime'}]
    });
	
	 //定义数据集 
    var newstore = Ext.create('Ext.data.Store', {
        model: 'worktime',
        autoLoad: false
    });
	
	//定义数据集 
    var userstore = Ext.create('Ext.data.Store', {
        model: 'User', 
        autoLoad: true,
        //使用ajax代理 
        proxy: { 
            type: 'ajax', 
            url: '../service/user_alluser',
            //Reader 解码json数据 
            reader: { 
                type: 'json', 
                root: 'root', 
                total: 'total'
            }
        }
    });
	
	var formadd=Ext.widget({
        xtype: 'form',
        layout: 'form',
        renderTo:'classset',
        title:'开设班级',
        width:500,
        frame:true,
        fieldDefaults: {
            msgTarget: 'qtip',
            labelWidth: 60
        },
        defaultType: 'textfield',
        items: [{
            fieldLabel: '班级名称',
            name: 'oaclass.className',
            allowBlank: false
        },{
            fieldLabel: '主教',
            xtype:'combobox',
            store:userstore,
            valueField:'id',
            editable:false,
            displayField: 'displayName',
            name: 'bishopid',
            allowBlank: false
        },{
            fieldLabel: '助教',
            name: 'assistantid',
            xtype:'combobox',
            store:userstore,
            editable:false,
            valueField:'id',
            displayField: 'displayName',
            allowBlank: false,
        },{
            fieldLabel: '课程',
            name: 'subjectid',
            xtype:'combobox',
            store:subjectstore,
            editable:false,
            valueField:'id',
            displayField: 'displayName',
            allowBlank: false,
        },{
            fieldLabel: '教室',
            name: 'oaclass.classroom',
            allowBlank: false,
        },{
            fieldLabel: '人数限制',
            name: 'oaclass.count',
            xtype: 'numberfield',
            minValue: 1,
            maxValue: 100,
            allowBlank: false,
        },{
            fieldLabel: '年龄限制',
            xtype:'combobox',
            store:agestore,
            valueField:'key',
            displayField: 'value',
            name: 'oaclass.age',
            editable:false,
            allowBlank: false,
        },{
            fieldLabel: '上课时间',
            id: 'worktimes',
            name: 'worktimes_str',
            hidden:true
        },Ext.create('Ext.grid.Panel', {
        	title:'上课时间列表',
        	store:newstore,
        	height:180,
            columns: [ 
            { 
                text: '上课时间', width: 200, sortable: true, dataIndex: 'worktime', editor: 'textfield'
            },{
            	text: '删除',
                xtype: 'actioncolumn',
                width: 50,
                items: [{
                    icon   : '../EXT4/fam/cross.gif"',  // Use a URL in the icon config
                    tooltip: '删除',
                    handler: function(grid, rowIndex, colIndex) {
                    	var rec = grid.getStore().getAt(rowIndex);
                    	Ext.MessageBox.confirm("确认","确认删除:"+rec.get('worktime'),function(btn){
                    		if(btn=='yes'){
                    			grid.getStore().removeAt(rowIndex);
                    			var s="";
                    			newstore.each(function(record) {
                        			s+=record.get('id')+",";
                        		});
                    			Ext.getCmp('worktimes').setValue(s);
                    		}  
                        }); 
                    }
                }]
            }],
            tbar : [{
            	id:'worktime',
            	xtype: 'textfield',
                width:200,
                regex:/^星期.+\d+[:：]\d+$/,
                regexText:'上课时间格式错误，示例:星期一 13:30',
                editable:false
            },{
                xtype : 'button',  
                text : '新增',
                handler: function() {
                	var comb=Ext.getCmp('worktime');
                    var re = /^星期.+\d+[:：]\d+$/;
                    if(re.test(comb.getValue())){
                		if(!comb.getValue()){
                    		Ext.Msg.alert('提示','请输入上课时间');
                    	}else if(newstore.find('id',comb.getValue())==-1){
                    		newstore.add({"id":comb.getValue(),"worktime":comb.getRawValue()});
                    		var s="";
                			newstore.each(function(record) {
                    			s+=record.get('id')+",";
                    		});
                			Ext.getCmp('worktimes').setValue(s);
                    	}else{
                    		Ext.Msg.alert('提示','相同时间已被添加');
                    	}
                	}else{
                		Ext.Msg.alert('提示','上课时间格式错误，示例:星期一 13:30');
                	}
                }
            }]
        })],
        buttons: [{
            	text: '确定',
            	handler: function() {
            		if(formadd.getForm().isValid()){
            			formadd.getForm().submit({  
                               url : '../service/class1_add',//请求的url地址  
                               method:'post',//请求方式
                               success:function(form,action){//加载成功的处理函数  
                              	Ext.Msg.alert('提示', action.result.msg);
                              	formadd.getForm().reset();
                               },
                               failure:function(form,action){//加载成功的处理函数  
               		        	Ext.Msg.alert('提示', action.result.msg); 
               		        	}
                           });
                  	 }
            	}
            },{
                text: '重置',
                handler: function() {
                	formadd.getForm().reset();
                	newstore.removeAll();
                }
            }]
     });
});
</script>
</head>
<body>
<div id="classset"></div>
</body>
</html>